import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyDialog {
  //底部弹出
  modalBottomSheet(context) async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                Text(
                  '你确定要退出吗？',
                  style: TextStyle(height: 2, fontSize: 30),
                ),
                ListTile(
                  title: Text('确定 '),
                  onTap: () {
                    Navigator.of(context).pop('确定');
                  },
                ),
                ListTile(
                  title: Text('取消 '),
                  onTap: () {
                    Navigator.of(context).pop('取消');
                  },
                )
              ],
            ),
          );
        });
    if (result != null) {
      Fluttertoast.showToast(msg: result);
    }
  }

  //弹出框
  Future<String> alertDialog(context) async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('提示信息'),
            content: Text("你确定要退出登录吗"),
            actions: [

              FlatButton(
                onPressed: () {
                  Navigator.of(context).pop('cancle');
                },
                child: Text('取消'),
              ),FlatButton(
                child: Text('确定'),
                onPressed: () {
                  Navigator.of(context).pop('ok');
                },
              ),
            ],
          );
        });
    return result;
  }

  //简单弹出框
  simpleDialog(context) async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("你确定要退出吗?"),
            children: [
              SimpleDialogOption(
                child: Text("确定"),
                onPressed: () {
                  Navigator.of(context).pop('确定');
                },
              ),
              SimpleDialogOption(
                child: Text("取消"),
                onPressed: () {
                  Navigator.of(context).pop('取消');
                },
              )
            ],
          );
        });
    Fluttertoast.showToast(msg: result);
  }

  //自定义弹出框
  customDialog(
      {context,
      @required child,
      title = '',
      content = '',
      height = 500.0,
      width = 500.0}) async {
    showDialog(
        context: context,
        builder: (context) {
          return CustomDialog(
              title: title,
              child: child,
              content: content,
              width: width,
              height: height);
        });
  }

  //自定义Loadding弹出框
  static loaddingDialog({context, title = '加载中', show = true}) async {
     await showDialog(
        context: context,
        builder: (context) {
          return CustomLoaddingDialog(
              height: 150.0, width: 150.0, title: title);
        });

    if (show == false) {
      Navigator.of(context).pop();
    }
  }

  static loaddingDialogWidget(title) {
    return CustomLoaddingDialog(height: 150.0, width: 150.0, title: title);
  }
}

class CustomLoaddingDialog extends Dialog {
  final double height;
  final double width;
  final String title;
  CustomLoaddingDialog({this.height, this.width, this.title});

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              padding: EdgeInsets.all(10),
              width: this.width,
              height: this.height,
              decoration: BoxDecoration(
                  color: Colors.black54,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
//                  CupertinoActivityIndicator(
//                      radius:50
//                  ),
                  CircularProgressIndicator(
//              value:null,
                    backgroundColor: Colors.grey.withAlpha(33),
                    valueColor: AlwaysStoppedAnimation(Colors.red),
                    strokeWidth: 5.0,
                  ),
                  SizedBox(height: 40.h),
                  Text(
                    title,
                    style: TextStyle(fontSize: 16.0, color: Colors.white),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class CustomDialog extends Dialog {
  final String title;
  final String content;
  final double width;
  final Widget child;
  final double height;
  CustomDialog({this.title, this.content, this.width, this.height, this.child});
  _showTimer(context) {
     Timer.periodic(Duration(seconds: 3), (t) {
      Navigator.of(context).pop();
      t.cancel();
    });
  }

  @override
  Widget build(BuildContext context) {
    _showTimer(context);
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              padding: EdgeInsets.all(10),
              width: this.width,
              height: this.height,
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Column(
                children: [
                  Align(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text('${this.title}'),
                        InkWell(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: Icon(Icons.close),
                        )
                      ],
                    ),
                    alignment: Alignment.topCenter,
                  ),
                  child,
                  Divider(),
                  Container(
                    width: double.infinity,
                    child: Text(
                      '${this.content}',
                      textAlign: TextAlign.left,
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
